<?php

namespace app\common\model\Business;

use think\Model;

class Msg extends Model
{
    //
    protected $name = "business_msg";

    //指定一个自动设置的时间字段
    //开启自动写入
    protected $autoWriteTimestamp = true;

    //设置字段的名字
    protected $createTime = "createtime"; //插入的时候设置的字段名

    //禁止 写入的时间字段
    protected $updateTime = false;


    protected $append = [
        'createtime_text',
        'status_text',
    ];

    public function getCreateTimeTextAttr($value,$data){
        $createtime = isset($data['createtime']) ? trim($data['createtime']) : '';
        if(empty($createtime)){
            return '';
        }
        return date('Y-m-d H:i',$createtime);
    }

    public function getStatusTextAttr($value,$data)
    {
        $statusList = [0 => '未读',1 => '已读'];
        $status = isset($data['status']) ? trim($data['status']) : '';
        if($status >= '0'){
            return $statusList[$status];
        }
    }

    public function business(){
        return $this->belongsTo('app\common\model\Business\User','busid','id',[],'LEFT')->setEagerlyType(0);
    }
}
